package controller

import (
	"errors"
	"fmt"
	"github.com/e421083458/golang_common/lib"
	"github.com/gin-gonic/gin"
	"tino_manager/dao"
	"tino_manager/dto"
	"tino_manager/middleware"
)

type MenuController struct {
}

func MenuRegister(group *gin.RouterGroup) {
	menu := &MenuController{}
	//创建菜单
	group.POST("/add", menu.Add)
	//获取菜单列表
	group.GET("/page", menu.Page)
	//获取菜单详情
	group.GET("/detail", menu.Detail)
	//修改菜单
	group.POST("/update", menu.Update)
	//删除菜单
	group.POST("/remove", menu.Remove)
}

// Add 新增菜单
func (m *MenuController) Add(c *gin.Context) {
	params := &dto.AddMenuInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败,请稍后重试"), true)
		return
	}
	menu := &dao.Menu{
		MenuName: params.MenuName,
		MenuPath: params.MenuPath,
		Icon:     params.Icon,
		PageKey:  params.PageKey,
	}
	if err := menu.Save(c, tx); err != nil {
		middleware.ResponseError(c, 500, errors.New("创建菜单失败,请稍后重试"), true)
		return
	}
	middleware.ResponseSuccess(c, "操作成功", true)
}

// Page 菜单列表
func (m *MenuController) Page(c *gin.Context) {
	params := &dto.MenuPageInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("获取数据失败，请稍后重试"), true)
		return
	}
	total := int64(0)
	offset := (params.PageNumber - 1) * params.PageSize
	var list []dao.Menu
	query := tx.WithContext(c).Model(&dao.Menu{}).Where("is_delete=0").Order("create_at desc")
	query.Count(&total)
	if err := query.Limit(params.PageSize).Offset(offset).Find(&list).Error; err != nil {
		middleware.ResponseError(c, 500, errors.New("获取数据失败，请稍后重试"), true)
		return
	}
	out := &dto.MenuPageOutput{
		Total: total,
	}
	for _, item := range list {
		out.List = append(out.List, dto.MenuInfo{
			MenuPath: item.MenuPath,
			MenuName: item.MenuName,
			Icon:     item.Icon,
			PageKey:  item.PageKey,
			Id:       fmt.Sprintf("%d", item.Id),
			Status:   item.Status,
		})
	}
	middleware.ResponseSuccess(c, out, false)
}

// Update 更新菜单
func (m *MenuController) Update(c *gin.Context) {
	params := &dto.UpdateMenuInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败，请稍后重试"), true)
		return
	}
	if result := tx.Model(&dao.Menu{}).Where(map[string]interface{}{
		"id": params.Id,
	}).Select("page_key", "menu_name", "menu_path", "icon", "status").Updates(&dao.Menu{
		PageKey:  params.PageKey,
		Icon:     params.Icon,
		MenuName: params.MenuName,
		MenuPath: params.MenuPath,
		Status:   params.Status,
	}); result.RowsAffected == 0 {
		middleware.ResponseError(c, 500, errors.New("操作失败，请稍后重试"), true)
		return
	}
	middleware.ResponseSuccess(c, "操作成功", true)
}

// Detail 菜单详情
func (m *MenuController) Detail(c *gin.Context) {
	params := &dto.MenuDetailInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("获取详情失败"), true)
		return
	}
	menu := &dao.Menu{}
	if result := tx.Where(map[string]interface{}{
		"id": params.Id,
	}).First(menu); result.RowsAffected == 0 {
		middleware.ResponseError(c, 500, errors.New("获取详情失败"), true)
		return
	}
	middleware.ResponseSuccess(c, dto.MenuInfo{
		Id:       fmt.Sprintf("%d", menu.Id),
		MenuPath: menu.MenuPath,
		MenuName: menu.MenuName,
		Icon:     menu.Icon,
		Status:   menu.Status,
		PageKey:  menu.PageKey,
	}, false)
}

// Remove 删除菜单
func (m *MenuController) Remove(c *gin.Context) {
	params := &dto.MenuRemoveInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("删除失败,请稍后重试"), true)
		return
	}
	if result := tx.Model(&dao.Menu{}).Where(map[string]interface{}{
		"id": params.Id,
	}).Update("is_delete", 1); result.RowsAffected == 0 {
		middleware.ResponseError(c, 500, errors.New("删除失败,请稍后重试"), true)
		return
	}
	middleware.ResponseSuccess(c, "操作成功", true)
}
